package com.bugstack.domain.strategy.service.rule.tree.impl;

import com.bugstack.domain.strategy.model.vo.RuleLogicCheckTypeVO;
import com.bugstack.domain.strategy.repository.IStrategyRepository;
import com.bugstack.domain.strategy.service.rule.tree.ILogicTree;
import com.bugstack.domain.strategy.service.rule.tree.factory.DefaultTreeFactory;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Date;

/**
 * @Author: czb
 * @Description: TODO
 * @DateTime: 2025/3/10 16:01
 **/

@Component("rule_lock")
public class RuleLockTreeNode implements ILogicTree {

    @Resource
    IStrategyRepository strategyRepository;

    @Override
    public DefaultTreeFactory.TreeActionEntity logic(String userId, Long strategyId, Integer awardId, String ruleValue, Date endDateTime) {
        Integer userCount = strategyRepository.queryDayRaffleCount(userId, strategyId);

        if (userCount >= Long.parseLong(ruleValue)) {
            return DefaultTreeFactory.TreeActionEntity.builder()
                    .strategyAwardVO(
                            DefaultTreeFactory.StrategyAwardVO.builder()
                                    .awardId(awardId)
                                    .awardRuleValue(ruleValue)
                                    .build()
                    )
                    .ruleLogicCheckType(RuleLogicCheckTypeVO.ALLOW)
                    .build();
        }
        return DefaultTreeFactory.TreeActionEntity.builder()
                .strategyAwardVO(
                        DefaultTreeFactory.StrategyAwardVO.builder()
                                .awardId(awardId)
                                .awardRuleValue(ruleValue)
                                .build()
                )
                .ruleLogicCheckType(RuleLogicCheckTypeVO.TAKE_OVER)
                .build();
    }
}
